package com.leetcode;

import java.util.Arrays;

/**
 * TwoArrayMergerAndSort.. 合并两有序数组 排序
 *
 * @author Lizhong
 * @date 2019/8/28
 */
public class TwoArrayMergerAndSort {

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        for (int i = 0; i < n; i++) {
            nums1[m + i] = nums2[i];
        }
        Arrays.sort(nums1);
    }

    /**
     * official，双指针方法
     * @param nums1
     * @param m
     * @param nums2
     * @param n
     */
    public void mergeOfficial(int[] nums1, int m, int[] nums2, int n) {

        int[] temp = new int[m];
        for (int i = 0; i < m; i++) {
            temp[i] = nums1[i];
        }
        int i = 0, j = 0;
        int p = 0;
        while (i < m && j < n) nums1[p++] = (temp[i] < nums2[j]) ? temp[i++] : nums2[j++];

        if (i < m) {
            for (int k = i; k < m; k++) {
                nums1[p++] = temp[k];
            }
        }

        if (j < n) {
            for (int k = j; k < n; k++) {
                nums1[p++] = nums2[k];
            }
        }


    }

}
